
#include "StateManager.h"
#include "ProgressManager.h"
#include "StateMainMenu.h"
#include "StateOptions.h"
#include "StateStats.h"
#include "defs.h"
#include "TextEntity.h"
#include "RenderableEntity.h"
#include "ScaleEffect.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "MusicManager.h"

StateOptions::StateOptions()
{
}

StateOptions::~StateOptions()
{
}

void StateOptions::loadContent()
{
    m_oBg = new RenderableEntity("MENU_0010_bg", Vector2(320, 480));
    m_oBg->setCentered();
    
    m_oTitleBg = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 870));
    m_oTitleBg->setCentered();
    m_oTitleBg->setLiving(true);
    
    m_oTitleText = new TextEntity("Options", FONT_NAME, 60, Vector2(320, 885), 1, 1);
    m_oTitleText->setCentered();
    m_oTitleText->setShadow(true, COLOR_PINK_DARK);
    m_oTitleText->setColor(COLOR_PINK_TEXT);
	
    m_oMusic = new TextEntity("Music", FONT_NAME, 48, Vector2(250, 700), 1, 1);
    m_oMusic->setCentered();
    m_oMusic->setShadow();
    m_oMusicBG = new RenderableEntity("checkbox", Vector2(420, 700));
	m_oMusicBG->setCentered();
    m_oMusicBG->setupButton(Vector2(1.5f, 1.5f));
    m_oMusicMark = new RenderableEntity("tick", Vector2(430, 710));
	m_oMusicMark->setCentered();

    m_oSfx = new TextEntity("Sounds", FONT_NAME, 48, Vector2(250, 550), 1, 1);
    m_oSfx->setCentered();
    m_oSfx->setShadow();
    m_oSfxBG = new RenderableEntity("checkbox", Vector2(420, 550));
	m_oSfxBG->setCentered();
    m_oSfxBG->setupButton(Vector2(1.5f, 1.5f));
    m_oSfxMark = new RenderableEntity("tick", Vector2(430, 560));
	m_oSfxMark->setCentered();
    
    m_oAccelerometer = new TextEntity("Accelerometer", FONT_NAME, 48, Vector2(230, 400), 1, 1);
    m_oAccelerometer->setCentered();
    m_oAccelerometer->setShadow();
    m_oAccelerometer->setColor(COLOR_GREY);
    m_oAccelerometerDisabled = new TextEntity("Buy it in the Shop first!", FONT_NAME, 24, Vector2(230, 360), 1, 1);
    m_oAccelerometerDisabled->setCentered();
    m_oAccelerometerDisabled->setShadow();
    m_oAccelerometerDisabled->setColor(COLOR_GREY);
    m_oAccelerometerBG = new RenderableEntity("checkbox", Vector2(420, 400));
	m_oAccelerometerBG->setCentered();
    m_oAccelerometerBG->setupButton(Vector2(1.5f, 1.5f));
    m_oAccelerometerMark = new RenderableEntity("tick", Vector2(430, 410));
	m_oAccelerometerMark->setCentered();
    
    m_oStats = new TextEntity("Statistics", FONT_NAME, 48, Vector2(320, 250));
    m_oStats->setCentered();
    m_oStats->setShadow();
    m_oStats->changeColorWhenClicked(true);
    
    m_oBack = new RenderableEntity("MENU_0008_back", Vector2(80, 80));
    m_oBack->setCentered();
    m_oBack->changeColorWhenClicked(true, COLOR_BLACK);
    m_oBack->setLiving(true);
    
    
	m_eOptionsEnter = new TimeLineEffect(false, false);
    m_eOptionsEnter->addInEffect(m_oTitleBg);
    m_eOptionsEnter->addInEffect(m_oTitleText);
    m_eOptionsEnter->addInEffect(m_oBack);
    m_eOptionsEnter->addInEffect(m_oMusic);
    m_eOptionsEnter->addInEffect(m_oMusicBG);
    m_eOptionsEnter->addInEffect(m_oMusicMark);
    m_eOptionsEnter->addInEffect(m_oSfx);
    m_eOptionsEnter->addInEffect(m_oSfxBG);
    m_eOptionsEnter->addInEffect(m_oSfxMark);
    m_eOptionsEnter->addInEffect(m_oAccelerometer);
    m_eOptionsEnter->addInEffect(m_oAccelerometerDisabled);
    m_eOptionsEnter->addInEffect(m_oAccelerometerBG);
    m_eOptionsEnter->addInEffect(m_oAccelerometerMark);
    m_eOptionsEnter->addInEffect(m_oStats);
    m_eOptionsEnter->addInEffect(m_oBack);
	
	m_eOptionsExit = new TimeLineEffect(false, false);
    m_eOptionsExit->addOutEffect(m_oTitleBg);
    m_eOptionsExit->addOutEffect(m_oTitleText);
    m_eOptionsExit->addOutEffect(m_oBack);
    m_eOptionsExit->addOutEffect(m_oMusic);
    m_eOptionsExit->addOutEffect(m_oMusicBG);
    m_eOptionsExit->addOutEffect(m_oMusicMark);
    m_eOptionsExit->addOutEffect(m_oSfx);
    m_eOptionsExit->addOutEffect(m_oSfxBG);
    m_eOptionsExit->addOutEffect(m_oSfxMark);
    m_eOptionsExit->addOutEffect(m_oAccelerometer);
    m_eOptionsExit->addOutEffect(m_oAccelerometerDisabled);
    m_eOptionsExit->addOutEffect(m_oAccelerometerBG);
    m_eOptionsExit->addOutEffect(m_oAccelerometerMark);
    m_eOptionsExit->addOutEffect(m_oStats);
    m_eOptionsExit->addOutEffect(m_oBack);
}

void StateOptions::unloadContent()
{
}

void StateOptions::enter()
{
	m_eState = Enter;
	m_eOptionsEnter->start(true);
    
    SaveGame sGame = ProgressManager::instance().getSavedData();
    m_oMusicMark->setRender(sGame.bMusic);
    m_oSfxMark->setRender(sGame.bSfx);
    m_oAccelerometerMark->setRender(sGame.bAccelerometer);
    
    //if(sGame.baUpgrades[ACCELEROMETER])
    if(true)
    {
        m_oAccelerometer->setColor(COLOR_WHITE);
        m_oAccelerometerDisabled->setRender(false);
    }
    else
    {
        m_oAccelerometer->setColor(COLOR_GREY);
        m_oAccelerometerDisabled->setRender(true);
    }
}

void StateOptions::update(float dt)
{
	switch(m_eState)
	{
		case Enter:
			if(m_eOptionsEnter->isFinished())
			{
				m_eState = Idle;
			}
			break;

		case Idle:
            m_oTitleBg->update();
            m_oTitleText->setScale(m_oTitleBg->getScale());
            
            m_oStats->update();
            m_oBack->update();

			if(m_oBack->isClicked())
			{
				m_eOptionsExit->start(true);
				m_eState = Exit;
			}
            else if(m_oMusicBG->isClicked() || m_oMusic->isClicked())
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bMusic = !sGame.bMusic;
                ProgressManager::instance().setSavedData(sGame);
                m_oMusicMark->setRender(sGame.bMusic);
                
                if(sGame.bMusic)
                    MusicManager::instance().setMusicVolume(MUSIC_VOLUME);
                else
                    MusicManager::instance().setMusicVolume(0);
            }
            else if(m_oSfxBG->isClicked() || m_oSfx->isClicked())
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bSfx = !sGame.bSfx;
                ProgressManager::instance().setSavedData(sGame);
                m_oSfxMark->setRender(sGame.bSfx);
            }
//            else if(ProgressManager::instance().getSavedData().baUpgrades[ACCELEROMETER] && (m_oAccelerometer->isClicked() || m_oAccelerometerBG->isClicked()))
            else if( m_oAccelerometer->isClicked() || m_oAccelerometerBG->isClicked() )
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bAccelerometer = !sGame.bAccelerometer;
                ProgressManager::instance().setSavedData(sGame);
                m_oAccelerometerMark->setRender(sGame.bAccelerometer);
            }
            else if(m_oStats->isClicked())
            {
                m_eOptionsExit->start(true);
				m_eState = ToStats;
            }
			break;
            
        case ToStats:
			if(m_eOptionsExit->isFinished())
			{
				StateManager::instance().setState(StateStats::instance());
			}
			break;
            
        case Exit:
			if(m_eOptionsExit->isFinished())
			{
				StateManager::instance().setState(StateMainMenu::instance());
			}
			break;
	}
}

void StateOptions::render()
{
    m_oBg->render();
    
    m_oTitleBg->render();
    m_oTitleText->render();
    
    m_oMusic->render();
    m_oMusicBG->render();
    m_oMusicMark->render();
    
    m_oSfx->render();
    m_oSfxBG->render();
    m_oSfxMark->render();
    
    m_oAccelerometer->render();
    m_oAccelerometerDisabled->render();
    m_oAccelerometerBG->render();
    m_oAccelerometerMark->render();
    
    //m_oReset->render();
    m_oStats->render();
    
    m_oBack->render();
}

void StateOptions::exit()
{
}
